//https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba?tpId=13&tqId=23254&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13


/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
#include <cstdlib>
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
        if(!pHead)
        {
            return nullptr;
        }
        RandomListNode* cur = pHead;
        unordered_map<RandomListNode*, RandomListNode*> map;
        //建立原-新映射
        while(cur != nullptr)
        {
            map[cur] = new RandomListNode(cur->label);
            cur = cur->next;
        }
        cur = pHead;
        //构建新链表的next和random指向
        while(cur !=  nullptr)
        {
            map[cur]->next = map[cur->next];
            map[cur]->random = map[cur->random];
            cur = cur->next;
        }

        return map[pHead];
    }
};